/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.jiaoyou.chats.service.impl;

import me.zhengjie.modules.jiaoyou.chats.domain.Chats;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.jiaoyou.chats.service.ChatsService;
import me.zhengjie.modules.jiaoyou.chats.domain.dto.ChatsQueryCriteria;
import me.zhengjie.modules.jiaoyou.chats.mapper.ChatsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author ztd
* @date 2025-11-19
**/
@Service
@RequiredArgsConstructor
public class ChatsServiceImpl extends ServiceImpl<ChatsMapper, Chats> implements ChatsService {

    private final ChatsMapper chatsMapper;

    @Override
    public PageResult<Chats> queryAll(ChatsQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(chatsMapper.findAll(criteria, page));
    }

    @Override
    public List<Chats> queryAll(ChatsQueryCriteria criteria){
        return chatsMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Chats resources) {
        chatsMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Chats resources) {
        Chats chats = getById(resources.getId());
        chats.copy(resources);
        chatsMapper.updateById(chats);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        chatsMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<Chats> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Chats chats : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("当前用户id", chats.getUserId());
            map.put("目标用户id", chats.getTargetId());
            map.put("最后消息", chats.getLastMessage());
            map.put("未读数量", chats.getUnreadCount());
            map.put("最后时间", chats.getLastTime());
            map.put(" createdAt",  chats.getCreatedAt());
            map.put(" updatedAt",  chats.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}